# -*- coding: utf-8 -*-
# #############################################################################
#    
#    Copyright (C) 2011 - TODAY Denero Team. (<http://www.deneroteam.com>)
#    All Rights Reserved
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
# Agredados campos y reacomodo de la vista. Leopo C.M. 10-jun-13
from osv import fields, osv


class mrp_production(osv.osv):
    """
    Production Orders / Manufacturing Orders
    """
    _inherit = 'mrp.production'

    def _calculate_total(self, cr, uid, ids, prop, unknow_none, context=None):
        res = {}
        for production in self.browse(cr, uid, ids, context=context):
            total_qty = 0
            for move_line2 in production.move_lines2:
                total_qty += move_line2.product_qty
            res[production.id] = total_qty
        return res

    _columns = {
        'total_qty': fields.function(_calculate_total, method=True, type='float', string='Total Teórico',
                                     help="Peso (gr) Teórico, tomado de la suma de los ingredientes consumidos al producir el producto"),
        'total_produced_qty': fields.float("Total Producido", required=True,
                                           help="Peso (gr) real del producto una vez finalizada la mezcla de los componentes."),
        'total_produced_net': fields.float("Total Neto Producido", readonly=True,
                                           help="Peso (gr) real del producto final menos el peso del recipiente"),
        'container_weight': fields.float("Peso del Recipiente", required=True,
                                         help="Peso (gr) del recipiente a utilizar."),
        'diff_qty_pqty': fields.float("Diferencia (gr)", readonly=True,
                                      help="Diferencia entre el peso teórico y práctico en gramos(gr)"),
        'diff_qty_porcentaje': fields.float("Diferencia en %", readonly=True,
                                            help="Tolerancia máxima. Positiva(+) 1.0% / Negativa(-) -0.5%"),
        'notes': fields.text('Notas de Producción'),
        'lot_state': fields.selection([('Aprobado', 'Aprobado'), ('Revisión', 'En Revisión'), ('Pruebas', 'De Prueba'),
                                       ('Desechado', 'Desechado')], 'Estado del Bache '),
        'qa_ph': fields.float("pH del Bache", help="pH medido para control, rango permitito entre 7.5 y 7.8"),
        'qa_densidad': fields.float("Densidad del Bache", help="Densidad medida para control"),
    }

    def calculate_diff(self, cr, uid, ids, fields, context=None):
        for production in self.browse(cr, uid, ids):
            difn = production.total_produced_qty - production.container_weight
            self.write(cr, uid, [production.id], {'total_produced_net': difn})
            diff = production.total_produced_net - production.total_qty
            porc = (production.total_produced_net / production.total_qty - 1) * 100
            self.write(cr, uid, [production.id], {'diff_qty_pqty': diff})
            self.write(cr, uid, [production.id], {'diff_qty_porcentaje': porc})
        return True


mrp_production()
 
